<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Comment extends MY_Controller {

	/**
	 * 产品模型
	 * @var ConsumerProductModel
	 */
	public $mProduct;
	
	/**
	 * 商户模型
	 * @var MerchantUserModel
	 */
	public $mMerchant;
	
	/**
	 * 产品评论模型
	 * @var MerchantCommentModel
	 */
	public $mComment;
	
	public function __construct(){
		parent::__construct();	
		$this->loadModel('ConsumerProductModel', 'mProduct', 'consumer');
		$this->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
		$this->loadModel('MerchantCommentModel', 'mComment', 'merchant');
	}

	/**
	 * 首页
	 */
	public function index(){

		$data = array(
				'status'=> $this->mProduct->getStatus(),
				'title'=>'商品评论管理',
		);
		
		$this->view('product/comment/home', $data, 'datatable,select2,validation,datepicker');
	}

    /**
     * 保存
     */
	public function saveReply(){
		//表单验证
		$vConf = array(
				array('comment', '平台回复内容', 'required'),
				array('id', '评论id', 'required|is_natural_no_zero'),
				
		);
		$this->formDataValidation($vConf);
	
		//参数
		$id 		= (int)post('id');
		$comment 	= trim(post('comment'));
		
		//是否存在
		if ( ! $this->mComment->getInfo($id)){
			$this->ajaxReturn(0,'评论不存在');
		}
		
		if ( ! $this->mComment->save(array('admin_reply'=>$comment),$id)){
			$this->ajaxReturn(0,'回复失败');
		}
		$this->ajaxReturn(1,'回复成功');
	}
	
	/**
	 * 获取列表
     * @author heyu
	 */
	public function getList(){
		$data = $this->mComment->adminGetList();

		foreach ($data['data'] as $k=>$v){
            $data['aaData'][$k][] = $v['m_name'];
		    $data['aaData'][$k][] = $v['name'];
			$data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item content textarea_height" style="height: 36px;">'.$v['content'].'</textarea>';
			$data['aaData'][$k][] = $v['score'];
			$data['aaData'][$k][] = $v['c_name'];
            //评语显示插件
            // 商家回复
			if (!empty($v['reply'])){
                $data['aaData'][$k][] = '<a href="#" class="popovers" data-placement="left" data-trigger="hover" data-content="'
                    .$v['reply'].'" data-original-title="商家回复">[商家回复]</a>';
            }else{
                $data['aaData'][$k][] = '';
            }
            // 平台回复
			if (!empty($v['admin_reply'])){
                $data['aaData'][$k][] = '<a href="#" class="popovers" data-placement="left" data-trigger="hover" data-content="'
                    .$v['admin_reply'].'" data-original-title="平台回复">[平台回复]</a>';
            }else{
                $data['aaData'][$k][] = '';
            }

			$data['aaData'][$k][] = date('Y-m-d H:i:s',$v['addtime']);
            $btnConfig = '';
            if(json_decode($v['imgs'])){
                $btnConfig = adminCreateBtn(array(
                    'label' => '评论图片',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'btn btn-xs green-stripe default img-btn',
                        'data-id' => $v['id'],
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'product',
                        'key' => 'comment-image'
                    )
                ));

            }

            if (empty($v['admin_reply'])){
                $btnConfig .=' '.adminCreateBtn(array(
                    'label' => '回复',
                    'attr' => array(
                        'href' => 'javascript:void(0)',
                        'class' => 'reply btn btn-xs blue-stripe default',
                        'data-id'=>$v['id'],
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'product',
                        'key' => 'comment-rely'
                    )
                ));
            }

            $btnConfig .= ' '.adminCreateBtn(array(
                'label' => '删除',
                'attr' => array(
                    'href' => getUrl('product/comment/rmComment?id='.$v['id']),
                    'class' => 'btn btn-xs red-stripe default ajax-request-btn',
                    'alert-message' => '是否确定刪除 “ '.$v['name'].' ” ?'
                ),
                'tag' => 'a',
                'permissions' => array(
                    'module' => 'product',
                    'key' => 'comment-delete'
                )
            ));
			$data['aaData'][$k][] = $btnConfig;
		}
		
		unset($data['data']);
		echo json_encode($data);
	}

    /**
     * 查看评论详情
     * @author heyu
     */
    public function getInfo(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');
        $info = $this->mComment->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '信息不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }
	
	//////////////////////////////////////////////////////////////////
	//+--------------------------------------------------------------
	//|	商品评论
	//+--------------------------------------------------------------
	
	/**
	 * 删除评价
	 */
	public function rmComment(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
		$id = (int)get('id');
        $info = $this->mComment->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '信息不存在或已删除');
        }
		if ( ! $this->mComment->removeComment($id)){
			 $this->ajaxReturn(0,'删除失败');
		}
		$this->ajaxReturn(1,'删除成功');
	}

}
